package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.Target;
import com.genexususa.eo.bing.data.GxItemException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class GxTarget extends Target {
    private long gxId;

    public GxTarget(Element element) throws IllegalArgumentException, GxItemException {
        if (element.getElementsByTagName("Age").getLength() > 0 ) {
            this.setAge(new GxAgeTarget((Element) element.getElementsByTagName("Age").item(0)));
        }
        if (element.getElementsByTagName("Day").getLength() > 0 ) {
            this.setDay(new GxDayTarget((Element)element.getElementsByTagName("Day").item(0)));
        }
        /*if (element.getElementsByTagName("Device").getLength() > 0 ) {
            this.setDevice(new GxDeviceTarget((Element)element.getElementsByTagName("Device").item(0)));
        }
        */
        if (element.getElementsByTagName("Gender").getLength() > 0 ) {
            this.setGender(new GxGenderTarget((Element)element.getElementsByTagName("Gender").item(0)));
        }
        if (element.getElementsByTagName("Hour").getLength() > 0 ) {
            this.setHour(new GxHourTarget((Element)element.getElementsByTagName("Hour").item(0)));
        }
        if (element.getElementsByTagName("GxTargetId").getLength() > 0 &&
            element.getElementsByTagName("GxTargetId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxTargetId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxId = Long.valueOf(((Node)element.getElementsByTagName("GxTargetId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external Target id");
        }
        if (element.getElementsByTagName("BingTargetId").getLength() > 0 &&
            element.getElementsByTagName("BingTargetId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingTargetId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setId(Long.valueOf(((Node) element.getElementsByTagName("BingTargetId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("IsLibraryTarget").getLength() > 0 &&
            element.getElementsByTagName("IsLibraryTarget").item(0).getChildNodes().getLength() > 0 &&
            !((Node)element.getElementsByTagName("IsLibraryTarget").item(0).getChildNodes().item(0)).getNodeValue().isEmpty()) {
            this.setIsLibraryTarget(Boolean.valueOf(((Node)element.getElementsByTagName("IsLibraryTarget").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("Location").getLength() > 0 ) {
            this.setLocation(new GxLocationTarget((Element)element.getElementsByTagName("Location").item(0)));
        }
        if (element.getElementsByTagName("Name").getLength() > 0 &&
            element.getElementsByTagName("Name").item(0).getChildNodes().getLength() > 0) {
            this.setName(((Node)element.getElementsByTagName("Name").item(0).getChildNodes().item(0)).getNodeValue());
        }
    }

    public long getGxId() {
        return gxId;
    }

    public void setGxId(long gxId) {
        this.gxId = gxId;
    }
}
